home *** CD-ROM | disk | FTP | other *** search
- Path: nntp-hub.barrnet.net!biosys!paralysys
- From: nasser@paralysys (Nasser Abbasi)
- Newsgroups: comp.lang.c++
- Subject: Re: Should I called exit() within method of a sub-object and return error info back to caller??
- Message-ID: <nh3f656z9w.fsf@paralysys>
- Date: 15 Apr 96 19:52:12 GMT
- References: <4ka0ta$ivj@tilde.csc.ti.com>
- Sender: news@biosys.apldbio.COM
- Organization: Applied BioSystems
- In-reply-to: Chuach@dlep1.itg.ti.com's message of 8 Apr 96 02:37:45 GMT
- To: Chuach@dlep1.itg.ti.com (Chua Chye Heng)
- X-Newsreader: Gnus v5.1
-
- In article <4ka0ta$ivj@tilde.csc.ti.com> Chuach@dlep1.itg.ti.com (Chua Chye Heng) writes:
-
- From: Chuach@dlep1.itg.ti.com (Chua Chye Heng)
-
- My scenario:
- Obj A uses Obj B to read data out from a definition file. When there
- is a serious error in the definition file, should I called exit inside
- the member function in Obj B or immediately return a BAD_STATUS
- indicator to the caller??
-
-
- The best way to handle errors in a program is to try to smoothly
- rewind the call stack all the way back to main, and back to the
- system level.
-
- This means that calling exit() from deep inside a large a program would
- not be a good idea, first of all, object destructors would not get
- called, (unless they are all global), and buffers would not get
- flushed out, files will not get closed, smoth landing will not happen !
-
- Each function should be responsible of cleaning after itself
- if an error occures, then return the status or signal an expection
- and let the caller frame do itself, etc.. untill the main line
- is reached.
-
- using exceptions or returning status are both better choices than
- calling exit() IMHO.
-
- In some implementation you can declare an exit handler, where by
- calling exit() would end up calling the exit handler where you can
- do some clean up work, but this is also not a very elegent way of
- doing things.
-
- Error handling in large programs is not an easy problem, returning
- error status has its advantages and disadvangtes, proper use of
- exception handling is considered the best way to handle errors
- in a program, but that also is not very easy technique to master,
- in particular if the exception handling model used by the language
- is complicated.
-
-
- Nasser
-